﻿using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

using SmartParking.Data.Entities;
using SmartParking.Bussiness;

namespace SmartParking.Web.Manager
{
    public partial class Managers : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                (Master as Logger).Title = "Managers";
                (Master as Logger).ActiveMenuItem = "Managers";
                LoadManagers();
                LoadCustomers();
                LoadRoles();
                upManager.Update();
            }
        }

        protected void LoadManagers()
        {
            SecurityManager.BindManagers(ref grdManagers);
        }

        protected void LoadRoles()
        {
            if (SecurityManager.IsSystemManager(Profile.ManagerID))
            {
                SecurityManager.BindRoles(ref ddlRole);
            }
            else
                rRole.Visible = false;
        }

        protected void LoadCustomers()
        {
            if (SecurityManager.IsSystemManager(Profile.ManagerID))
            {
                ParkingManager.BindCustomers(ref ddlCustomer);
            }
            else
                rCustomer.Visible = false;
        }

        protected void btnSubmitManager_Click(object sender, EventArgs e)
        {
            SecurityManager.SaveManager(
                hfIdManager.Value,
                (!string.IsNullOrEmpty(ddlCustomer.SelectedValue) ? Convert.ToInt32(ddlCustomer.SelectedValue) : SecurityManager.GetManager(Profile.ManagerID).CustomerID),
                (!string.IsNullOrEmpty(ddlRole.SelectedValue) ? Convert.ToInt32(ddlRole.SelectedValue) : SecurityManager.GetManager(Profile.ManagerID).RoleID),
                txtUserName.Text,
                txtPassword.Text,
                txtEmail.Text,
                txtPhone.Text,
                Profile.ManagerID,
                Request.UserHostAddress);

            CloseDialog("pnlManagerForm");
            CleanManagerForm();
            LoadManagers();
        }

        /// <summary>
        /// Registers client script to close the dialog
        /// </summary>
        /// <param name="dialogId"></param>
        private void CloseDialog(string dialogId)
        {
            string script = string.Format(@"closeDialog('{0}')", dialogId);
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
        }

        /// <summary>
        /// Registers client script to close the dialog
        /// </summary>
        /// <param name="dialogId"></param>
        private void OpenDialog(string dialogId)
        {
            string script = string.Format(@"showDialog('{0}')", dialogId);
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
        }

        protected void grdManager_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditManager")
            {
                SmartParking.Data.Entities.Manager manager = SecurityManager.GetManager(Convert.ToInt32(e.CommandArgument));
                if (manager != null)
                {
                    hfIdManager.Value = e.CommandArgument.ToString();
                    txtUserName.Text = manager.UserName;
                    txtPassword.Text = manager.Password;
                    txtEmail.Text = manager.Email;
                    txtPhone.Text = manager.Phone;

                    if (SecurityManager.IsSystemManager(Profile.ManagerID))
                    {
                        ddlCustomer.SelectedValue = manager.CustomerID.ToString();
                        ddlRole.SelectedValue = manager.RoleID.ToString();
                    }
            
                    btnSubmitManager.Text = "Update";
                    lblManagerTitle.Text = "Modify Customer";
                    btnDeleteManager.Visible = true;
                    upManagerForm.Update();
                    OpenDialog("pnlManagerForm");
                }
            }
            else if (e.CommandName == "DeleteManager")
                DeleteManager(Convert.ToInt32(e.CommandArgument));
        }

        private void DeleteManager(int id)
        {
            SecurityManager.DeleteManager(id, Profile.ManagerID, Request.UserHostAddress);
            LoadManagers();
        }

        protected void btnDeleteManager_Click(object sender, EventArgs e)
        {
            DeleteManager(Convert.ToInt32(hfIdManager.Value));
            CleanManagerForm();
            CloseDialog("pnlManagerForm");
            upManager.Update();
        }

        protected void btnCancelManager_Click(object sender, EventArgs e)
        {
            CleanManagerForm();
            CloseDialog("pnlManagerForm");
        }

        private void CleanManagerForm()
        {
            btnSubmitManager.Text = "Save";
            lblManagerTitle.Text = "Add Manager";
            btnDeleteManager.Visible = false;

            txtUserName.Text = txtPassword.Text = txtPhone.Text = txtEmail.Text = hfIdManager.Value = String.Empty;
            ddlCustomer.SelectedIndex = ddlRole.SelectedIndex = -1;
        }
    }
}
